package com.electron.power.model.mod;

import java.time.LocalDateTime;

import org.springframework.beans.BeanUtils;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@ApiModel("数据体")
@NoArgsConstructor
@Document(collection = "modBodyMap")
public class ModBodyMap {
	public static final Integer DEVICE_ID_LENTH = 5;

	@Id
	private String id;

	@ApiModelProperty(value = "设备编号,长度5", example = "112394521950")
	private long deviceId;

	@ApiModelProperty(value = "A相数据")
	private ModMapData modMapDataOfA;
	
	@ApiModelProperty(value = "B相数据")
	private ModMapData modMapDataOfB;
	
	@ApiModelProperty(value = "C相数据")
	private ModMapData modMapDataOfC;

	private LocalDateTime creatTime;
	
	private LocalDateTime lastTime;

	@ApiModelProperty(value = "crc校验位,长度2字节", example = "12093")
	private int crc;

	/**
	 * 将数据清洗成人能看懂的数据
	 * 
	 * @param mod
	 * @return
	 */
	public static ModBodyMap exchange(ModRoot mod) {
		ModBodyMap modBodyMap = new ModBodyMap();
		ModBody modBody = mod.getBody();
		ModData modDataOfA = modBody.getModDataOfA();
		ModMapData modMapData = ModMapData.wash(modDataOfA);
		modBodyMap.setModMapDataOfA(modMapData);
		
		ModData modDataOfB = modBody.getModDataOfB();
		modMapData = ModMapData.wash(modDataOfB);
		modBodyMap.setModMapDataOfB(modMapData);
		
		ModData modDataOfC = modBody.getModDataOfC();
		modMapData = ModMapData.wash(modDataOfC);
		modBodyMap.setModMapDataOfC(modMapData);
		
		
//		BeanUtils.copyProperties(mod.getBody(), modBodyMap);
		modBodyMap.setId(mod.getId());
		modBodyMap.setDeviceId(mod.getBody().getDeviceId());
		modBodyMap.setCrc(mod.getBody().getCrc());
		modBodyMap.setCreatTime(LocalDateTime.now());
		modBodyMap.setLastTime(LocalDateTime.now());
		return modBodyMap;
	}
	
	/**
	 * 将数据清洗成人能看懂的数据
	 * 
	 * @param mod
	 * @return
	 */
	public static ModBodyMap exchange(ModStr mod) {
		ModBodyMap modBodyMap = new ModBodyMap();
		
		Integer[] modStrArray = {1,1,2,5,1,32,32,32,2};
		Integer[] modDataArray = {4,4,4,4,4,4,4,4};
		String modStrData = mod.getData();
		
		String[] arrangeStrBody = arrangeData(modStrData,modStrArray);
		for(int i=0 ; i<arrangeStrBody.length;i++){
			System.out.println(arrangeStrBody[i]);
		}
		String dataA = arrangeStrBody[5];
		Integer[] arrangeModOfA = arrangeMod(dataA, modDataArray);
		ModMapData modMapDataOfA = ModMapData.wash(arrangeModOfA);
		modBodyMap.setModMapDataOfA(modMapDataOfA);
		
		String dataB = arrangeStrBody[6];
		Integer[] arrangeModOfB = arrangeMod(dataB, modDataArray);
		ModMapData modMapDataOfB = ModMapData.wash(arrangeModOfB);
		modBodyMap.setModMapDataOfB(modMapDataOfB);
		
		String dataC = arrangeStrBody[7];
		Integer[] arrangeModOfC = arrangeMod(dataC, modDataArray);
		ModMapData modMapDataOfC = ModMapData.wash(arrangeModOfC);
		modBodyMap.setModMapDataOfC(modMapDataOfC);
		
		
		modBodyMap.setId(mod.getId());
		modBodyMap.setDeviceId(Long.valueOf(arrangeStrBody[3],16));
		modBodyMap.setCrc(Integer.parseInt(arrangeStrBody[8], 16));
		modBodyMap.setCreatTime(LocalDateTime.now());
		modBodyMap.setLastTime(LocalDateTime.now());
		return modBodyMap;
	}
	
	/**
	 * 将网络传输过来的字符串按照每个字段定义的长度转成对应的数组.
	 * @param dataBody
	 * @return
	 */
	public static Integer[] arrangeMod(String dataBody,Integer[] lenthArray) {
		Integer[] result = new Integer[lenthArray.length];
		int position = 0;
		for(int i=0 ; i<lenthArray.length;i++){
			String sub = dataBody.substring(position,position+lenthArray[i]*2);
			System.out.println(sub);
			result[i]=Integer.parseInt(sub, 16);
			position +=lenthArray[i]*2;
		}
		return result;
	}
	
	/**
	 * 将网络传输过来的字符串按照每个字段定义的长度转成对应的数组.
	 * @param dataBody
	 * @return
	 */
	public static String[] arrangeData(String dataBody,Integer[] lenthArray) {
		String[] result = new String[lenthArray.length];
		int position = 0;
		for(int i=0 ; i<lenthArray.length;i++){
			result[i] = dataBody.substring(position,position+lenthArray[i]*2);
			position +=lenthArray[i]*2;
		}
		return result;
	}
	
	
	public static void main(String[] args) {
//		String modData = "00000000000000000000000000000000000003E70000000000000DF700001388";
//		Integer[] lenthArray = {4,4,4,4,4,4,4,4};
//		Integer[] modlenthArray = {1,1,2,5,1,32,32,32,2};
//		String s = "550300661A2B3C4D6E8000000000000000000000000000000000000003E70000000000000DF70000138800000000000000000000000000000000000003E70000000000000D930000138800000000000000000000000000000000000003E70000000000000D6100001388E906550300661A2B3C4D6E8000000000000000000000000000000000000003E70000000000000DF70000138800000000000000000000000000000000000003E70000000000000D7A0000138800000000000000000000000000000000000003E70000000000000D7A00001388579E";
//		String[] arrangeBody = arrangeData(s,modlenthArray);
//		for(int i=0 ; i<arrangeBody.length;i++){
//			System.out.println(arrangeBody[i]);
//		}
//		String dataA = arrangeBody[5];
//		Integer[] arrangeMod = arrangeMod(dataA, lenthArray);
//		for(int j = 0;j<arrangeMod.length;j++) {
//			System.out.println(arrangeMod[j]);
//		}
		
		String str = "8800000";
		int parseInt = Integer.parseInt(str, 16);
		System.out.println(parseInt);
	}
}
